<?php
add_action('wp_head', 'feedback_submit');
function feedback_submit(){?>
<script type="text/javascript">
    jQuery(document).ready(function($) {
        function isEmail(emailAddress) {
            var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
            return pattern.test(emailAddress);
        };
        function back_to_top(){
            var duration = 500;
                jQuery('html, body').animate({
                scrollTop: 0}, duration);
        }
        var frm = $('#contact');
        frm.submit(function(event){
            event.preventDefault();
            $('input[name="send_feedback"]').attr('disabled','disabled');
            $('div.loading_feedback').removeClass('hidden');
            var name = $('input[name="txt_yourname"]').val();
            var email = $('input[name="txt_email_contact"]').val();
            var title = $('input[name="txt_title"]').val();
            var content = $('textarea[name="txt_content_contact"]').val();
            var captcha = $('input[name="txt_captcha"]').val();
            var value_sending = [name,email,title,content,captcha];
            var data ={
                    action : 'feedback',
                    value_send : value_sending
                };
                jQuery.post(ajaxurl, data, function(result) {
                    if(result == 'captcha not match'){
                        $('div.captcha-check-fail').removeClass('hiden');
                    }
                    if(result == 'insert_fail'){
                        back_to_top();
                        $('input[name="send_feedback"]').removeAttr('disabled');
                        $('div.loading_feedback').addClass('hidden');
                        $('.contac-quest p:first').removeClass('hidden');
                        $('.contac-quest p:last').addClass('hiden');
                        $('div.insert_err').removeClass('hiden');
                    }
                    if(result == 'insert_success'){
                        back_to_top();
                        $('input[name="send_feedback"]').removeAttr('disabled');
                        $('div.loading_feedback').addClass('hidden');
                        //
                        $('.contac-quest p:first').addClass('hidden');
                        //remove field
                        $('input[name="txt_yourname"]').val('');
                        $('input[name="txt_email_contact"]').val('');
                        $('input[name="txt_title"]').val('');
                        $('textarea[name="txt_content_contact"]').val('');
                        $('input[name="txt_captcha"]').val('');

                        $('.contac-quest p:last').removeClass('hiden');
                    }
                });
        });
        $('input[name="txt_email_contact"]').blur(function(){
            if($(this).val().length == 0){
                $(this).addClass('error_input');
            }else{
                $(this).removeClass('error_input');
            }
            if(!isEmail($(this).val()) && $(this).val().length > 0){
                $('span.email_not_valid_contact').removeClass('hiden');
            }else{
                $('span.email_not_valid_contact').addClass('hiden');
            }
        });
        $('input[name="txt_yourname"]').blur(function(){
            if($(this).val().length == 0){
                $(this).addClass('error_input');
            }else{
                $(this).removeClass('error_input');
            }
        });
        $('input[name="txt_title"]').blur(function(){
            if($(this).val().length == 0){
                $(this).addClass('error_input');
            }else{
                $(this).removeClass('error_input');
            }
        });
        $('textarea[name="txt_content_contact"]').blur(function(){
            if($(this).val().length == 0){
                $(this).addClass('error_input');
            }else{
                $(this).removeClass('error_input');
            }
        });
        $('button[name="send_feedback"]').click(function(){
            var name = $('input[name="txt_yourname"]');
            var email = $('input[name="txt_email_contact"]');
            var title = $('input[name="txt_title"]');
            var content = $('textarea[name="txt_content_contact"]');
            var captcha = $('input[name="txt_captcha"]');
            if(name.val().length == 0){
                name.addClass('error_input');
            }else{
                name.removeClass('error_input');
            }
            if(email.val().length == 0){
                email.addClass('error_input');
            }else{
                email.removeClass('error_input');
            }
            if(title.val().length == 0){
                title.addClass('error_input');
            }else{
                title.removeClass('error_input');
            }
            if(content.val().length == 0){
                content.addClass('error_input');
            }else{
                content.removeClass('error_input');
            }
            if(captcha.val().length == 0){
                captcha.addClass('error_input');
            }else{
                captcha.removeClass('error_input');
            }
            if(name.val().length != 0 && email.val().length != 0 && title.val().length != 0 && content.val().length != 0 && captcha.val().length != 0){
                return true;
            }
            return false;
        });
    });
</script>
<?php }
add_action('wp_ajax_nopriv_feedback', 'feedback_callback');
add_action('wp_ajax_feedback', 'feedback_callback');
function feedback_callback(){
    $email_receive = get_option('email_contact');
    $arr_mails = explode(',',$email_receive);

    global $wpdb,$table_prefix;
    session_start();
    $value_received = $_POST['value_send'];
    $name = $value_received[0];
    $email = $value_received[1];
    $title = $value_received[2];
    $content = $value_received[3];
    $captcha = $value_received[4];
    $time = date("Y-m-d G:i:s",time() + 25200);
    //if($captcha != 0 && $_SESSION['6_letters_code'] == $captcha){
        if($name !='' && $email != '' && $title != '' && $content != '' && $captcha!=''){
            $my_query ="
                    INSERT INTO ".$table_prefix."feedback(send_by,send_email,title,send_content,TIME)
                    VALUE('$name','$email','$title','$content','$time')
                ";
            $wpdb->get_results($my_query);
            // send alert email
            $em_content = "Title: ".$title."<br/>"."Customer email: ".$email."<br/>Message: \"".$content."\"<br/>View detail: ".admin_url("/admin.php?page=customer-contact")."<br/>Time: ".$time;
            $em_title = "New contact from: ".$name;
            foreach ($arr_mails as $m) {
                wp_mail($m,$em_title,$em_content);
            }
            echo 'insert_success';
        }else{
            echo 'insert_fail';
        }
//    }else{
//        echo 'captcha not match';
//    }
    die();
}
?>
